package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.FreemarkerService;
import com.heima.common.exception.CustomException;
import com.heima.file.service.impl.MinIOTemplate;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.pojos.SearchArticleVo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;


/**
 * @author xiaquanen
 * @version 1.0
 * @project heima-leadnews
 * @date 2024/7/18 21:52:14
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private FreemarkerService freemarkerService;
    /**
     * 文章查看
     *
     * @param
     * @param target
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, int target) {
        List<ApArticle> apArticles = apArticleMapper.load(dto, target);
        return ResponseResult.okResult(apArticles);
    }

    /**
     * 同步文章信息
     *
     * @param articleDto
     * @return
     */
    @Override
    public ResponseResult syncArticle(ArticleDto articleDto) {
        if(articleDto.getId()==null)
        {
            //新增操作三张表
             //新增文章表
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(articleDto,apArticle);

            apArticleMapper.insert(apArticle);
            //将生成的文章id传给articleDto中
            articleDto.setId(apArticle.getId());
            //新增配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            //新增文章详情
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else
        {
            //更新操作
            //更新两张张表
            //更新文章表
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(articleDto,apArticle);

            apArticleMapper.updateById(apArticle);
            //更新文章内容表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.update(apArticleContent, Wrappers.<ApArticleContent>lambdaUpdate().eq(ApArticleContent::getArticleId,apArticle.getId()));
        }
        //创建文件模版,使用异步执行，提高接口的响应速度
        freemarkerService.createFreemarker(articleDto);
        return ResponseResult.okResult(String.valueOf(articleDto.getId()));
    }

}
